var express = require('express');
var router = express.Router();
var datafile = require('../file_views/data');

router.get('/index', function (req, res, next) {
  console.log('start')
  datafile.getData(function (err, data) {
    if (err) {
      return res.status(500).send('Server error');
    }
    res.render('index', {
      finished_data: data.finished,
      unfinished_data: data.unfinished
    });
  })

});
router.get('/getdata', function (req, res, next) {
  datafile.getData(function (err, data) {
    if (err) {
      return res.status(500).send('Server error');
    }
    res.json({
      title: 'Express',
      finished_data: data.finished,
      unfinished_data: data.unfinished
    });
  })
});

router.post('/upload', function (req, res, next) {
  var data = req.body.data;
  console.log('data',req.body.data)
  datafile.uploadData(data,function (err, data) {
    if (err) {
      return res.status(500).send('Server error');
    }
    if(data == 'success'){
      res.json({result:'上传成功'});
    }
  })
});
module.exports = router;